@file:Suppress("MemberVisibilityCanBePrivate")

package com.virtualightning.mvvm.base

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder

/**
 *  Anchor : Create by CimZzz
 *  Time : 2021/10/16 13:36
 *  Service 简单封装的基类
 */
abstract class BaseService: Service() {
	protected var isRunning: Boolean = false

	override fun onCreate() {
		super.onCreate()
		initService()
	}

	override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
		if(!isRunning)
			runService(intent)
		else onRepeatRunService(intent)
		return super.onStartCommand(intent, flags, startId)
	}

	override fun onDestroy() {
		markRest()
		destroyService()
		super.onDestroy()
	}


	override fun onBind(intent: Intent?): IBinder = Binder()

	fun markRunning() {
		isRunning = true
	}

	fun markRest() {
		isRunning = false
	}

	abstract fun initService()
	abstract fun runService(intent: Intent?)
	open fun onRepeatRunService(intent: Intent?) {}
	abstract fun destroyService()
}